import pygame
from pygame import Rect
import random

class ZoomBie():

    # live：生命值，img_num:该僵尸动图拆分的PNG数量
    # width、height分别为png的宽高
    def __init__(self,screen,live,img_num,width,height):
        self.live = live
        self.width = width
        self.height = height

        # 生命为1是普通僵尸
        if self.live == 1:
            zoombie_image = 'resource/image/normal_zoombie_115x140x17.png'
        elif self.live == 2:
            zoombie_image = 'resource/image/cone_head_zoombie_115x140x17.png'
        surface = pygame.image.load(zoombie_image).convert_alpha()
        self.image_lst = [surface.subsurface(Rect((i * width, 0), (width, height)))
                          for i in range(img_num)]
        self.img_num = img_num
        self.img_index = 0
        # 僵尸生成的初始位置随机，初始速度随机
        self.pos_x = 1024
        self.pos_y = random.randint(0,372)
        self.v_x = random.randint(-6,-2)
        self.v_y = random.randint(-6,6)
        # 每一帧图像停留的时间(ms)
        self._rate = 100
        self.screen = screen
        # 创建一个时钟对象用来控制帧率
        self.clock = pygame.time.Clock()
        self.passed_time = 0

    # 专门用来刷新动图的函数,返回下一次要输出的img_surface
    def update(self):
        passed_time = self.clock.tick()
        self.passed_time += passed_time

        if self.passed_time >= self._rate:
            self.img_index += 1
            self.passed_time = 0
        
        if self.img_index == self.img_num-1:
            self.img_index = -1

        return self.image_lst[self.img_index]

    def move(self):
        # 如果已经走出上下边界，速度反向
        if self.pos_y <= 0 or self.pos_y >= 372:
            self.v_y *= -1
        # x生成向左的随机速度
        self.v_x = random.randint(-6,-2)
        self.pos_x += self.v_x
        self.pos_y += self.v_y
    
    def draw(self):
        img = self.update()
        self.screen.blit(img, (self.pos_x, self.pos_y))

class HotDog:
    def __init__(self,screen,x,y):
        self.pos_x = x + 51 #为了热狗对象能和王校长的嘴对上，所以加51/13进行调整
        self.pos_y = y + 13
        self.v_x = random.randint(5,10)
        self.v_y = random.randint(-10,10)
        self.screen = screen
        self.img = pygame.image.load('resource/image/hotdog_52x32.png').convert_alpha()
        self.transform_flag = 1
        self.width = 52
        self.height = 32
        # 刚射出的热狗离wxz太近会被判定为击中校长,所以用时间控制刚射出的热狗不会击中校长
        self.clock = pygame.time.Clock()
        self.passed_time = 0
    
    def move(self):

        self.pos_x += self.v_x
        self.pos_y += self.v_y

        if self.pos_x >= 972:
            self.v_x *= -1
        if self.pos_y <= 0 or self.pos_y >= 480:
            self.v_y *= -1
        self.passed_time += self.clock.tick()
    
    def hit(self):
        self.img = pygame.image.load('resource/image/hit_120x90.png').convert_alpha()
        # 为了爆炸的图片能和爆炸前热狗图片重合，添加29/34的调整
        self.pos_y -= 29
        self.pos_x -= 34
        self.draw()

    
    def draw(self):
        self.screen.blit(self.img, (self.pos_x, self.pos_y))


class WXZ:
    def __init__(self,screen):
        self.img = pygame.image.load('resource/image/wxz_110x70.png').convert_alpha()
        self.pos_x = 0
        self.pos_y = 221
        self.screen = screen
        # 键盘控制wxz时的移动速度
        self.v = 12
        # 热狗列表，用来存放发射出来的热狗对象
        self.hotdog_lst = []
        self.width = 110
        self.height = 35
        self.score = 0
    
    def shoot(self):
        # 随机发射1~4个热狗
        n = random.randint(1,4)
        for i in range(n):
            self.hotdog_lst.append(HotDog(self.screen,self.pos_x,self.pos_y))
    
    def keyhandle(self,cmd):
        if cmd == 'left':
            if self.pos_x > 0:
                self.pos_x -= self.v
        if cmd == 'right':
            if self.pos_x < 914:
                self.pos_x += self.v
        if cmd == 'up':
            if self.pos_y > 0:
                self.pos_y -= self.v
        if cmd == 'down':
            if self.pos_y < 442:
                self.pos_y += self.v            
        
        if cmd == 'space':
            self.shoot()
    
    def draw(self):
        self.screen.blit(self.img, (self.pos_x, self.pos_y))
